﻿@model CategoryNavigationModel
@using Nop.Admin.Models.Catalog;
@* <tianyingchun> generate category navigation static view template avoid each time exec categories. *@
@functions{
    public bool BreadCrumbContainsCurrentCategoryId(CategorySimpleModel category, int level)
    {
        // always return 2 level categories.
        // <tianyingchun always return category tree.
        if (level < 1) //2 equals return root level and first child level.
        {
            return true;
        }

        //if (Model.CurrentCategoryId == 0)
        //    return false;

        //if (category.Id == Model.CurrentCategoryId)
        //    return true;

        //foreach (var subCategory in category.SubCategories)
        //{
        //    if (BreadCrumbContainsCurrentCategoryId(subCategory, level))
        //    {
        //        return true;
        //    }
        //}

        return false;
    }
}
@helper RenderCategoryLine(CategorySimpleModel category, int level)
{
    var hasChilds = (category.Id == Model.CurrentCategoryId
        || BreadCrumbContainsCurrentCategoryId(category, level))
        && category.SubCategories.Count > 0;

        <li class="@(category.Id == Model.CurrentCategoryId ? "active" : "inactive")">
            @{
                var classes = new List<string>();
                if (category.ShowOnHomePage)
                {
                    classes.Add("hot");
                }
                if (hasChilds)
                {
                    classes.Add("has-children");
                }
                var classStr = classes.Count > 0 ? string.Join(" ", classes) : "";
            }
            <a class="@(classStr)" href="@Url.RouteUrl("Category", new { SeName = category.SeName })">
                @category.Name
                @if (category.NumberOfProducts.HasValue)
                {
                    <text> </text>@T("Categories.TotalProducts", category.NumberOfProducts.Value)
                }
                @if (category.ShowOnHomePage)
                {
                    <span class="hot"></span>
                }
                @if (hasChilds)
                {
                    <span class="arrow"></span>
                }
            </a>
            @{
                if (hasChilds)
                {
                    <ul class="sublist">
                        @foreach (var subCategory in category.SubCategories)
                        {
                            @RenderCategoryLine(subCategory, level + 1)
                        }
                    </ul>
                }
            }
        </li>
}
@if (Model.Categories.Count > 0)
{
    <div class="block category-navigation-tree">
        @*<div class="title">
                <strong>@T("Categories")</strong>
            </div>*@
        <div class="listbox">
            <ul class="list-unstyled">
                @foreach (var category in Model.Categories)
                {
                    // start from 0.
                    @RenderCategoryLine(category, 0);
                }
            </ul>
        </div>
    </div>
}